<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class UserCenterApiController extends Controller
{
    /**
     * 获取用户信息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function profile(Request $request): JsonResponse
    {
        try {
            // 模拟用户数据
            $user = [
                'id' => 10086123,
                'name' => '张小明',
                'avatar' => 'https://example.com/avatar.jpg',
                'phone' => '138****8888',
                'membership_level' => 'gold',
                'membership_name' => '黄金会员',
                'created_at' => '2024-01-01',
                'last_login' => '2025-08-16 10:30:00'
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $user
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取钱包信息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function wallet(Request $request): JsonResponse
    {
        try {
            $type = $request->get('type', 'personal'); // personal 或 enterprise

            // 模拟钱包数据
            $wallet = [
                'type' => $type,
                'balance' => $type === 'personal' ? 1280.50 : 25600.00,
                'frozen_amount' => 0.00,
                'total_income' => $type === 'personal' ? 5000.00 : 100000.00,
                'currency' => 'CNY',
                'last_updated' => '2025-08-16 15:30:00'
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $wallet
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取云仓信息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function cloudStorage(Request $request): JsonResponse
    {
        try {
            $type = $request->get('type', 'personal'); // personal 或 enterprise

            // 模拟云仓数据
            $storage = [
                'type' => $type,
                'used_space' => $type === 'personal' ? 2.5 : 15.8, // GB
                'total_space' => $type === 'personal' ? 10.0 : 100.0, // GB
                'file_count' => $type === 'personal' ? 128 : 1024,
                'last_backup' => '2025-08-16 14:00:00'
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $storage
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取商家管理菜单
     *
     * @return JsonResponse
     */
    public function merchantMenu(): JsonResponse
    {
        try {
            $menu = [
                [
                    'id' => 'store_application',
                    'name' => '门店申请',
                    'icon' => '🏢',
                    'description' => '申请开设新门店',
                    'status' => 'available'
                ],
                [
                    'id' => 'pickup_application',
                    'name' => '自提点申请',
                    'icon' => '📍',
                    'description' => '申请成为自提点',
                    'status' => 'available'
                ],
                [
                    'id' => 'redemption_code',
                    'name' => '置换码',
                    'icon' => '🎫',
                    'description' => '管理置换码',
                    'status' => 'available'
                ]
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $menu
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取门店管理菜单
     *
     * @return JsonResponse
     */
    public function storeMenu(): JsonResponse
    {
        try {
            $menu = [
                [
                    'id' => 'replacement_center',
                    'name' => '置换中心',
                    'icon' => '🔄',
                    'description' => '管理商品置换',
                    'status' => 'available'
                ],
                [
                    'id' => 'store_orders',
                    'name' => '门店订单',
                    'icon' => '📋',
                    'description' => '查看订单信息',
                    'status' => 'available'
                ],
                [
                    'id' => 'product_list',
                    'name' => '商品清单',
                    'icon' => '⭐',
                    'description' => '管理商品信息',
                    'status' => 'available'
                ],
                [
                    'id' => 'pc_backend',
                    'name' => 'PC端后台登录网址',
                    'icon' => '💻',
                    'description' => '访问PC管理后台',
                    'status' => 'available',
                    'url' => 'http://yidui.jiujiefang7979.cn/'
                ]
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $menu
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }

    /**
     * 获取实用工具
     *
     * @return JsonResponse
     */
    public function tools(): JsonResponse
    {
        try {
            $tools = [
                [
                    'id' => 'forgot_password',
                    'name' => '忘记密码',
                    'icon' => '🔑',
                    'description' => '重置登录密码',
                    'status' => 'available'
                ]
            ];

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $tools
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '服务器错误',
                'data' => null
            ], 500);
        }
    }
}

